#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :guotie_shop.py
# Time  :2024/12/18 15:01
# Email :fortunatedong@163.com
# Author:by Adonli
import json
import random
import time

import requests
class GetShopSku():
    def __init__(self):
        self.item_url = 'https://mall.95306.cn/proxy/elasticsearch-service/mall/search/queryItemListByKeywordNew'
        self.sku_detail_url = 'https://mall.95306.cn/proxy/item/mall/search/queryNormalItemDetails'
        self.sku_price_url = 'https://mall.95306.cn/proxy/item/mall/search/querySkuPrice'
    def guotie_item_url_header(self,page_item_count):
        item_header = {
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
            "Cookie":"AlteonPmall=xxxxxxxxxxxxx; st=f4e3004c3702af376d3972ba6a3e925b; cookieFinger=1742786814847",
            "Authorization":"xxxxxxxxxxxxxxxxxxx"#动态变化
        }
        item_payload = {
            "platformId":20,
            "shopInfoId": 202202224106,
            "materialType": 0,
            "pageNum":page_item_count
        }
        return item_header, item_payload
    def guotie_sku_detail_url_header(self,itemId):
        sku_detail_header = {
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
            "Cookie":"AlteonPmall=xxxxxxxx; st=f4e3004c3702af376d3972ba6a3e925b; cookieFinger=1742786814847",
            "Authorization":"xxxxxxxxxxxx"#动态变化
        }
        sku_detail_payload = {
            "platformId":20,
            "itemId": itemId,
            "areaId": -1
        }
        return sku_detail_header, sku_detail_payload
    def guotie_sku_detail_price_url_header(self,skuId):
        sku_detail_header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
            "Cookie": "AlteonPmall=0a03b7f7004606581f41; st=f4e3004c3702af376d3972ba6a3e925b; cookieFinger=1742786814847",
            "Authorization": "xxxxxxxxxxxxxxx"  # 动态变化
        }
        sku_detail_payload = {
            "platformId": 20,
            "skuIds": skuId
        }
        return sku_detail_header, sku_detail_payload
    def get_item_json(self,page_item_count):
        item_header, item_payload = self.guotie_item_url_header(page_item_count)
        response = requests.get(self.item_url, headers=item_header, params=item_payload)
        return response.json()
    def get_sku_detail_json(self,itemId):
        sku_detail_header, sku_detail_payload = self.guotie_sku_detail_url_header(itemId)
        response = requests.get(self.sku_detail_url, headers=sku_detail_header, params=sku_detail_payload)
        return response.json()
    def get_sku_detail_price_json(self,skuId):
        sku_detail_price_header, sku_detail_price_payload = self.guotie_sku_detail_price_url_header(skuId)
        response = requests.get(self.sku_price_url, headers=sku_detail_price_header, params=sku_detail_price_payload)
        return response.json()

    def get_item_id(self):
        item_json = self.get_item_json(page_item_count=1)
        # product_json = json.dumps(product_json,ensure_ascii=False)
        item_count = item_json["data"]["itemList"]["count"]
        if item_count % 30 == 0:
            page_item_count = item_count // 30
        else:page_item_count = item_count // 30 + 1
        get_items = []
        for i in range(1,page_item_count+1):
            item_json = self.get_item_json(page_item_count=i)
            get_sku_list = item_json["data"]["itemList"]["resultList"]
            for j in get_sku_list:
                get_items.append(j["itemId"])
            time.sleep(random.randint(1,3))
        get_items_set = set(get_items)
        return get_items_set

    def get_sku_detail(self):
        get_items_set = self.get_item_id()
        get_skus = []
        for i in get_items_set:
            sku_detail_json = self.get_sku_detail_json(i)
            sku_brand_name = sku_detail_json["data"]["itemBarCodeVoList"][0]["brandName"]
            sku_fcname = sku_detail_json["data"]["itemPublishVo"]["frontCategoryVoList"][-1]["fcname"]
            for j in sku_detail_json["data"]["itemSkuInfoList"]:
                item_id = j["itemId"]
                sku_id = j["id"]
                sku_attributes_name = j["attributesName"]
                sku_model_code =  j["modelCode"]
                sku_name = j["skuName"]
                sku_unit = j["skuUnit"]
                sku_spec_attributes = j["specAttributes"]
                sku_tax_Rate = j["taxRate"]
                sku_uptime = j["upTime"]
                get_skus.append({"item_id":item_id,"sku_id":sku_id,"sku_brand_name":sku_brand_name,"sku_fcname":sku_fcname,"sku_attributes_name":sku_attributes_name,
                                 "sku_model_code":sku_model_code,"sku_name":sku_name,"sku_unit":sku_unit,
                                 "sku_spec_attributes":sku_spec_attributes,"sku_tax_Rate":sku_tax_Rate,"sku_uptime":sku_uptime})
            time.sleep(random.randint(1,3))
        return get_skus
    def get_sku_detail_price(self):
        get_skus = self.get_sku_detail()
        get_skus_price = []
        for i in get_skus:
            get_sku_price = self.get_sku_detail_price_json(i["sku_id"])
            i["sku_market_price"] = get_sku_price["data"][0]["marketPrice"]
            i["sku_sell_price"] = get_sku_price["data"][0]["sellPrice"]
            get_skus_price.append(i)
        return get_skus_price

    def save_get_sku_detail_price(self):
        get_skus = self.get_sku_detail_price()
        guotie_skus = {
            "sku":get_skus
        }
        with open('data/guotie_skus_detail.json', 'w') as f:
            json.dump(guotie_skus, f, ensure_ascii=False, indent=4)





if __name__ == '__main__':
    GetShopSku().save_get_sku_detail_price()